//
//  HDRecognizeeMultiImageUploadCell.swift
//  HuaDaoPeifu
//
//  Created by Jeremy on 2018/7/16.
//  Copyright © 2018年 Jeremy. All rights reserved.
//

import UIKit
// import HXPhotoPicker

class HDRecognizeeMultiImageUploadCell: UITableViewCell {

    var model:HDRecognizeeMultiImageUploadCellModel?
    var photoViewChangeHeightBlock:((_ cell:HDRecognizeeMultiImageUploadCell)->Void)?

    let manager = HXPhotoManager(type: HXPhotoManagerSelectedTypePhoto)
    let toolManager = HXDatePhotoToolManager.init()
    var photoView:HXPhotoView?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupUI()
    }
    
    fileprivate func setupUI() {
        self.selectionStyle = .none
        photoView = HXPhotoView(frame: CGRect(x: AppConstant.screenWidth - 10-300, y: 10, width: 300, height: 0), manager: manager)
        photoView?.manager.configuration.photoMaxNum = 1000
        photoView?.manager.configuration.navBarBackgroudColor = UIColor.white
        photoView?.hideDeleteButton = true
        photoView?.editEnabled = false
        photoView?.previewShowDeleteButton = false
        photoView?.manager.configuration.photoCanEdit = false
        photoView?.delegate = self
        self.contentView.addSubview(photoView!)
        
        self.model = HDRecognizeeMultiImageUploadCellModel()
        /// 单元格默认的高度
        model?.cellHeight = self.bounds.height
        model?.photoViewHeight = self.bounds.height
        
    }
}

extension HDRecognizeeMultiImageUploadCell:HXPhotoViewDelegate{
    
    func photoView(_ photoView: HXPhotoView!, updateFrame frame: CGRect) {
        if frame.size.height == ((self.model?.photoViewHeight)!-20) {
            return
        }
        self.model?.photoViewHeight = frame.size.height + 20
        if (self.photoViewChangeHeightBlock != nil) {
            self.photoViewChangeHeightBlock!(self)
        }
    }
    
    func photoView(_ photoView: HXPhotoView!, changeComplete allList: [HXPhotoModel]!, photos: [HXPhotoModel]!, videos: [HXPhotoModel]!, original isOriginal: Bool) {
        model?.allPhotos = photos
        var localPhotos = [HXPhotoModel]()
        for photo in photos {
            if photo.networkPhotoUrl == nil {
                localPhotos.append(photo)
            }
        }
        
        toolManager.getSelectedImageList(localPhotos, success: {[unowned self] (list:[UIImage]?) in
            
            if let list = list {
                self.model?.photos = list
            }
        }) {
        }
    }
}

class HDRecognizeeMultiImageUploadCellModel: NSObject {
    var cellHeight:CGFloat = 0
    var photoViewHeight:CGFloat = 0
    var photos:[UIImage]?
    var allPhotos = [HXPhotoModel]()
}
